import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'

const routes = [
  {
    path: '/',
    name: '登录',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/registered',
    name: '注册',
    component: () => import('@/views/registered/index.vue'),
  },
  {
    path: '/forgetPass',
    name: '忘记密码',
    component: () => import('@/views/forgetPass/index.vue'),
  },
  {
    path: '/changePass',
    name: '修改手机号',
    component: () => import('@/views/changePass/index.vue'),
  },
  {
    path: '/editPass',
    name: '修改密码',
    component: () => import('@/views/editPass/index.vue'),
  },
  {
    path: '/home',
    name: '主页',
    component: () => import('@/views/home/index.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: '欢迎',
        component: () => import('@/views/welcome/index.vue'),
      },
      {
        path: '/userManage',
        name: '员工管理',
        redirect: '/userList',
        children: [
          {
            path: '/userList',
            name: '员工列表',
            component: () => import('@/views/userManage/list/index.vue'),
            children: [],
          },
          {
            path: '/userRole',
            name: '角色权限',
            component: () => import('@/views/userManage/role/index.vue'),
            children: [],
          },
        ],
      },
      {
        path: '/goodsManage',
        name: '商品管理',
        component: () => import('@/views/goodsManage/index.vue'),
        children: [
          {
            path: '/goodsManage/add',
            name: '新增商品',
            component: () => import('@/views/goodsManage/add.vue'),
          },
          {
            path: '/goodsManage/detail',
            name: '商品详情',
            component: () => import('@/views/goodsManage/detail.vue'),
          },
        ],
      },
      {
        path: '/shopManage',
        name: '代理商管理',
        component: () => import('@/views/shopManage/index.vue'),
        children: [
          {
            path: '/shopManage/detail',
            name: '代理商详情',
            component: () => import('@/views/shopManage/detail.vue'),
          },
          {
            path: '/shopManage/add',
            name: '代理商新增',
            component: () => import('@/views/shopManage/add.vue'),
          },
        ],
      },
      {
        path: '/subordinate',
        name: '我的商铺',
        component: () => import('@/views/subordinate/index.vue'),
        children: [],
      },
      {
        path: '/orderManage',
        name: '订单管理',
        redirect: '/orderSale',
        children: [
          {
            path: '/orderSale',
            name: '销售订单',
            component: () => import('@/views/orderManage/sale/index.vue'),
            children: [
              {
                path: '/orderSale/detail',
                name: '销售订单详情',
                component: () => import('@/views/orderManage/sale/detail.vue'),
              }
            ],
          },
          {
            path: '/orderJoin',
            name: '加盟订单',
            component: () => import('@/views/orderManage/join/index.vue'),
            children: [
              {
                path: '/orderJoin/detail',
                name: '加盟订单详情',
                component: () => import('@/views/orderManage/join/detail.vue'),
              }
            ],
          },
          {
            path: '/orderRecharge',
            name: '充值订单',
            component: () => import('@/views/orderManage/recharge/index.vue'),
            children: [
              {
                path: '/orderRecharge/detail',
                name: '充值订单详情',
                component: () => import('@/views/orderManage/recharge/detail.vue'),
              }
            ],
          },
          {
            path: '/rechargeRefund',
            name: '退款订单',
            component: () => import('@/views/orderManage/rechargeRefund/index.vue'),
            children: [
              {
                path: '/rechargeRefund/detail',
                name: '退款订单详情',
                component: () => import('@/views/orderManage/rechargeRefund/detail.vue'),
              }
            ],
          },
          {
            path: '/orderWithdraw',
            name: '提现订单',
            component: () => import('@/views/orderManage/withdraw/index.vue'),
            children: [
              {
                path: '/orderWithdraw/detail',
                name: '订单详情',
                component: () => import('@/views/orderManage/withdraw/detail.vue'),
              }
            ],
          },
          {
            path: '/orderIncome',
            name: '佣金订单',
            component: () => import('@/views/orderManage/income/index.vue'),
            children: [
              {
                path: '/orderIncome/detail',
                name: '佣金订单详情',
                component: () => import('@/views/orderManage/income/detail.vue'),
              }
            ],
          },
        ],
      },
      {
        path: '/incomeManage',
        name: '佣金管理',
        redirect: '/commission',
        children: [
          {
            path: '/commission',
            name: '佣金列表',
            component: () => import('@/views/incomeManage/commission/index.vue'),
            children: [],
          },
          {
            path: '/incomeShop',
            name: '商铺佣金',
            component: () => import('@/views/incomeManage/incomeShop/index.vue'),
            children: [],
          },
          {
            path: '/incomeRule',
            name: '佣金规则',
            component: () => import('@/views/incomeManage/incomeRule/index.vue'),
            children: [
              {
                path: '/incomeRule/add',
                name: '新增佣金规则',
                component: () => import('@/views/incomeManage/incomeRule/add.vue'),
              },
              {
                path: '/incomeRule/detail',
                name: '佣金规则详情',
                component: () => import('@/views/incomeManage/incomeRule/detail.vue'),
              },
            ],
          },
        ],
      },
      {
        path: '/bannerMng',
        name: '轮播图管理',
        component: () => import('@/views/bannerMng/index.vue'),
      },
      {
        path: '/softwareRelease',
        name: '版本管理',
        component: () => import("@/views/softwareRelease/index.vue"),
        redirect: '/softwareRelease/projectManagement',
        children: [
          {
            path: '/softwareRelease/projectManagement',
            name: '项目管理',
            component: () => import("@/views/softwareRelease/projectManagement/index.vue"),
          },
          {
            path: '/softwareRelease/products',
            name: '套餐列表',
            component: () => import("@/views/softwareRelease/products/index.vue"),
          },
          {
            path: '/softwareRelease/softwareManagement',
            name: '软件管理',
            component: () => import("@/views/softwareRelease/softwareManagement/index.vue"),
          },
        ]
      },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

const wtiterPath = ['/']
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = window.localStorage.getItem('USER_TOKEN_INRE')
  if (!token && !wtiterPath.includes(to.path)) {
    next({ path: '/' })
    return
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
